package com.zfj.HashSet;

import java.sql.SQLOutput;
import java.util.HashSet;
import java.util.Iterator;

/**
 * @author zfj
 * @date 2022/3/7 -14:53
 * hashSet使用
 * 存储结构：哈希表(数组+链表+红黑树)
 * 根据hashcode计算保存位置，如果位置为空，则直接保存，如果不为空执行第二步
 * 执行equals方法，如果equals方法为true，
 */
public class Demo2 {
	public static void main(String[] args) {
		HashSet<Person> people=new HashSet<>();
		//添加元素
		Person p1=new Person("张三",20);
		Person p2=new Person("李四",20);
		Person p3=new Person("王五",20);
		people.add(p1);
		people.add(p2);
		people.add(p3);
		people.add(new Person("张三",20));
		System.out.println(people.toString());
		//删除元素
		people.remove(p1);
		//遍历
		for (Person person : people) {
			System.out.println(person);
		}
		System.out.println("=========迭代器=========");
		Iterator<Person> iterator = people.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		System.out.println(people.contains(new Person("李四", 20)));
		System.out.println(people.isEmpty());
	}
}
